//
//  HttpEngine.m
//  KouDaiMaster
//
//  Created by 范智渊 on 2018/4/11.
//  Copyright © 2018年 zzgk. All rights reserved.
//

#import "HttpEngine.h"
#import "dataMenagerTool.h"
#import "KDMAPPConfig.h"
@implementation HttpEngine
/** 请求头配置*/
+ (AFHTTPSessionManager *)shareHeader {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 10;

    //申明返回的结果是json类型
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    //请求头设置
    [manager.requestSerializer setValue:AppKey forHTTPHeaderField:@"AppKey"];
    //随机字符串
    NSString *NonceStr =[dataMenagerTool getRandomStringWithNum:6];
    [manager.requestSerializer setValue: NonceStr forHTTPHeaderField:@"Nonce"];
    //获取1970年到现在的时间秒数
    NSString *curTimeStr = [dataMenagerTool getCurTimeSecondStr];
    [manager.requestSerializer setValue:curTimeStr forHTTPHeaderField:@"CurTime"];
    //获取checkSum值
    NSString *checkSumStr = [dataMenagerTool sha1:AppSecret,NonceStr,curTimeStr, nil];
    [manager.requestSerializer setValue:checkSumStr forHTTPHeaderField:@"CheckSum"];

    //    static dispatch_once_t onceToken;
    //    dispatch_once(&onceToken, ^{
    //    NSLog(@"Appkey ----- %@\n NonceStr ------- %@\n curTimeStr------- %@\n checkSumStr ------%@",AppKey,NonceStr,curTimeStr,checkSumStr);
    //    });
    return manager;

}

+ (AFHTTPSessionManager *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void (^)(NSURLSessionDataTask * task, id responseObject))success failure:(void (^)(NSURLSessionDataTask * task, NSError *error))failure {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.class BlockPOST:URLString parameters:parameters success:success failure:failure];
    });
    return nil;
}

+(void)BlockPOST:(NSString *)URLString
      parameters:(NSDictionary *)parameters
         success:(void (^)(NSURLSessionDataTask * task, id responseObject))success
         failure:(void (^)(NSURLSessionDataTask * task, NSError *error))failure
{

    void (^failureBlock)(NSURLSessionDataTask * task, NSError *error)=^(NSURLSessionDataTask * task, NSError *error){
        if(error.code==1000)
            {
            // NSLog(@"error-1000  -  %@",error.description);
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                //  NSLog(@"error-1000 - %@",URLString.copy);
                [self.class BlockPOST:URLString.copy parameters:parameters.copy success:[success copy] failure:[failure copy]];
            });
            }else{
                failure(task, error);
            }

    };


       AFHTTPSessionManager *manager = [HttpEngine shareHeader];
    [manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task,responseObject);



    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         failureBlock(task, error);
    }];
}

@end

